import time

import requests

from tools.base_page import BasePage, Base_Api


class SuanfaManage(BasePage):
    def suanfa_query(self, name, username):
        #  我的算法查询
        self.open_yaml_element('../data/suanfa_manage.yaml', 'suanfa_input', is_elements=1, ele_num=0, content=name)
        self.open_yaml_element('../data/suanfa_manage.yaml', 'suanfa_input', is_elements=1, ele_num=1, content=username)
        self.open_yaml_element('../data/suanfa_manage.yaml', 'suanfa_type')
        suanfa_type_option = 'body>div:nth-of-type(3)>div>div>div>ul>li:nth-of-type(6)'
        time.sleep(2)
        self.selector_execute_script(suanfa_type_option)
        self.elementclickintercepted(self.only_read_yaml('../data/suanfa_manage.yaml', 'suanfa_query_button'))
        time.sleep(2)
        first_suanfa_list = self.only_read_yaml('../data/suanfa_manage.yaml', 'first_suanfa_list')
        return self.find(first_suanfa_list).text

    def add_suanfa(self,name):
        # 添加算法
        self.base = Base_Api()
        login_data = {
            'method': "post",
            "url": self.base.host + '/sso/auth/login',
            "files": {
                "username": (None, "ceshi"),
                "password": (None, "123456")
            }
        }

        r = self.base.send(login_data)
        satoken = r.json()['data']['satoken']

        add_data = {
            "method": "post",
            "url": self.base.host + '/analysev2/algorithmProcess/addAlgo',
            "headers": {"Content-Type": "application/json", "Cookie": f"satoken={satoken}"},
            "json": {"name":name,"codeEntrance":"func","type":"1","label":"测试","markdown":"测试","explanationScope":"随机对照临床试验RCT","dependenceArr":[{"id":0,"dependenceName":"","install":""}],"inParam":[{"id":0}],"inVariable":[{"id":0}],"outVariable":[{"id":0}],"coreValue":["0"],"functionCodes":[{"language":"R","codeType":"0","codePos":"/root/process_data/algorithm/389b8f06dab74174bec7b1dcdefe799e/1644457531072/8fc657ad63d0c369.jpg"}],"explanationCheck":"0"}
        }
        add = self.base.send(add_data)
        return add

    def suanfa_publish(self):
        # 发布算法
        name=f"name{round(time.time(), 0)}"
        self.add_suanfa(name)
        self.driver.refresh()
        time.sleep(2)
        self.open_yaml_element('../data/suanfa_manage.yaml','fabu')
        sh_status_locator = self.only_read_yaml('../data/suanfa_manage.yaml','sh_status')
        sh_status_status = self.find(sh_status_locator).text
        return sh_status_status

    def delect_df(self):

        self.driver.refresh()
        time.sleep(2)
        self.open_yaml_element('../data/suanfa_manage.yaml','fabu',is_elements=1,ele_num=2) # 删除
        self.verify_wait_click(self.only_read_yaml('../data/suanfa_manage.yaml','delect_locator'))
        self.open_yaml_element('../data/suanfa_manage.yaml','delect_locator')
        time.sleep(2)



        first_suanfa_list = self.only_read_yaml('../data/suanfa_manage.yaml','first_suanfa_list')
        first_suanfa_text = self.find(first_suanfa_list).text
        return first_suanfa_text


